package Offer.offer2020;

import domain.ListNode;

import java.util.Stack;

/**
 * 从头到尾打印链表
 */
public class Test05 {
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode p1 = new ListNode(2);
        ListNode p2 = new ListNode(3);
        ListNode p3 = new ListNode(4);

        head.next = p1;
        p1.next = p2;
        p2.next = p3;
        p3.next = null;

        printListReversely(head);
    }

    public static void printListReversely(ListNode head) {
        if (head == null) {
            return;
        }
        Stack<Integer> stack = new Stack<>();
        ListNode p = head;
        while (p != null) {
            stack.add(p.val);
            p = p.next;
        }

        while (!stack.isEmpty()) {
            System.out.println(stack.pop());
        }
    }
}
